Skip to content

fix: bound inline embedding so a slow provider cannot fail the write - #99

Merged
acidkill merged 1 commit into
mainfrom
fix/inline-embed-timeout
Jul 28, 2026
Merged

acidkill merged 1 commit into
mainfrom
fix/inline-embed-timeout

Conversation

@acidkill

Copy link
Copy Markdown
Owner

Closes #79.

Problem

_embed_created_neurons runs inside the write path so a fresh memory is semantically recallable immediately. It is already fail-soft about a missing provider — "keyword-only memory, no error, no slowdown", as its own docstring promises — but a slow provider was treated differently: the call blocked until the provider's own timeout.

Against a remote or rate-limited endpoint (the reporter runs a proxied Gemini endpoint) that pushes smem_remember past the 30s tool-call cap MCP hosts such as Claude Code and Claude Desktop impose. The neuron has already been persisted by then, so the caller sees a timeout and cannot tell whether the write landed — precisely the loss this tool exists to prevent. The same content through the CLI succeeds, because nothing caps it there.

Verified still present on main (d8233b1) before changing anything: encoder.py:438 awaits _embed_created_neurons, which awaits provider.embed_batch with no bound. No async/pending-embedding path exists (git grep pending_embedding is empty).

Change

The embedding call runs under asyncio.wait_for with a 10s budget — well above a local endpoint (bge-m3 via llamastash answers in ~15 ms) and well below the 30s cap, so the bound only ever fires on a provider that would have broken the call anyway.

On timeout the memory stays keyword-only and logs a warning naming smem reindex as the back-fill. That is the same outcome the method already produced when no provider was available — this extends an existing guarantee to a case it did not cover, rather than inventing new behaviour.

SURREAL_MEMORY_INLINE_EMBED_TIMEOUT tunes it; <= 0 restores the previous unbounded behaviour for anyone who prefers it.

Scope

This is the minimal form of option 2 in the issue. It deliberately does not implement ack-first with async back-fill (option 1) or a pending_embedding state (option 3) — both are larger changes worth their own discussion. This closes the data-loss window in the meantime, and does not foreclose either.

Test plan

  • tests/unit/test_inline_embed_timeout.py — 5 cases: default sits under the 30s cap, env override, <= 0 disables, garbage falls back instead of raising, and a hanging provider does not block the write
  • Test proven to have teeth: with the bound disabled the same scenario hangs (confirmed by running it with SURREAL_MEMORY_INLINE_EMBED_TIMEOUT=0 — the call never returns), so this fails without the fix
  • pytest tests/unit — 6563 passed, 48 skipped
  • ruff check / ruff format --check clean across src/ and tests/

Closes #79.

`_embed_created_neurons` runs inside the write path so a fresh memory is
semantically recallable immediately. It was already fail-soft about a *missing*
provider — "keyword-only memory, no error, no slowdown", as its docstring
promises — but a *slow* provider was treated differently: the call blocked until
the provider's own timeout.

Against a remote or rate-limited endpoint that pushes `smem_remember` past the
30s tool-call cap MCP hosts impose. The neuron has already been persisted at that
point, so the caller sees a timeout and cannot tell whether the write landed —
precisely the loss this tool exists to prevent. The same content via the CLI
succeeds, because nothing caps it there.

The embedding call now runs under `asyncio.wait_for` with a 10s budget: well
above a local endpoint (bge-m3 via llamastash answers in ~15 ms) and well below
the 30s cap. On timeout the memory stays keyword-only and logs a warning naming
`smem reindex` as the back-fill, which is the same outcome the method already
produced when no provider was available. Set
`SURREAL_MEMORY_INLINE_EMBED_TIMEOUT` to tune it, or to <= 0 to restore the
previous unbounded behaviour.

This is the minimal form of option 2 in the issue. It does not implement
ack-first/async back-fill (option 1) — that is a larger change and worth its own
discussion; this closes the data-loss window in the meantime.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

remember blocks on synchronous embedding → times out on MCP clients with a bounded tool timeout

1 participant